import Vue from "vue";
import storage from "@/utils/storage";

export const state = {
    systemInfo: null,
};

export const mutations = {
    SET_SYSTEM_INFO: (state, value) => {
        state.systemInfo = value;
    },
};

export const actions = {
    // 获取用户终端系统信息
    SystemInfo({ commit, state }) {
        return new Promise((resolve, reject) => {
            if (state.info) {
                resolve(res);
            } else {
                uni.getSystemInfo({
                    success(e) {
                        commit("SET_SYSTEM_INFO", e);
                        // 信息存入缓存（有效期1天）
                        storage.set("SYSTEM_INFO", e, 86400);

                        // #ifndef MP
                        Vue.prototype.StatusBar = e.statusBarHeight;
                        if (e.platform == "android") {
                            Vue.prototype.CustomBar = e.statusBarHeight + 50;
                        } else {
                            Vue.prototype.CustomBar = e.statusBarHeight + 44;
                        }
                        // #endif
                        // 微信小程序版同步处理
                        // #ifdef MP-WEIXIN
                        Vue.prototype.StatusBar = e.statusBarHeight;
                        let custom = wx.getMenuButtonBoundingClientRect();
                        Vue.prototype.Custom = custom;
                        Vue.prototype.CustomBar =
                            custom.bottom + custom.top - e.statusBarHeight + 8;
                        // #endif

                        resolve(e);
                    },
                    fail(err) {
                        reject(err);
                    },
                });
            }
        });
    },
};

export const getters = {
    getSystemInfo(state) {
        if (state.systemInfo) {
            return state.systemInfo;
        } else if (storage.get("SYSTEM_INFO")) {
            return JSON.parse(storage.get("SYSTEM_INFO"));
        }
        return {};
    },
};
